﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;

namespace Communergy.UnitTesting
{
    public static class UnitTestHelper
    {


        //public static string ConnectionString ;
        private static Random random = new Random((int)DateTime.Now.Ticks);

        public static int RandomInt(int max)
        {
            return random.Next(max);
        }

        public static int RandomInt()
        {
            return random.Next();
        }

        public static string RandomString(int strLength)
        {
            string randomString = "";

            for (int x = 0; x < strLength; x++) randomString += (char)(random.Next(65, 90));
            return randomString;
        }


        public static DateTime RandomDateTime()
        {
            return RandomDateTime(DateTime.Parse("18/02/1971"), DateTime.Parse("21/12/2012"));
            ;

        }


        /// <summary>
        /// Returns DateTime in the range [min, max)
        /// </summary>
        public static DateTime RandomDateTime(DateTime min, DateTime max)
        {
            if (max <= min)
            {
                string message = "Max must be greater than min.";
                throw new ArgumentException(message);
            }
            long minTicks = min.Ticks;
            long maxTicks = max.Ticks;
            double rn = (Convert.ToDouble(maxTicks)
               - Convert.ToDouble(minTicks)) * random.NextDouble()
               + Convert.ToDouble(minTicks);
            return new DateTime(Convert.ToInt64(rn));

        }


        public static TimeSpan RandomTimeSpan()
        {
            TimeSpan randomDateTime = new TimeSpan(UnitTestHelper.RandomInt(23), UnitTestHelper.RandomInt(59), UnitTestHelper.RandomInt(59));

            return randomDateTime;// DateTime.Now.AddMinutes((double)RandomInt());
        }

        public static bool RandomBool()
        {

            bool randBool = random.NextDouble() > 0.5;
            return Convert.ToBoolean(randBool);
        }


        public static DateTime RandomTime()
        {
            string randomTimeString = string.Concat(UnitTestHelper.RandomInt(23).ToString("00"), ":", UnitTestHelper.RandomInt(59).ToString("00"), ":", UnitTestHelper.RandomInt(59).ToString("00"));
            DateTime randomTime = new DateTime(0, 0, 0, UnitTestHelper.RandomInt(23), UnitTestHelper.RandomInt(59), UnitTestHelper.RandomInt(59));
            return randomTime;
        }


        public static int LineCounter(string source)
        {

            Regex RE = new Regex("\n", RegexOptions.Multiline);
            MatchCollection theMatches = RE.Matches(source);
            var lines = theMatches.Count + 1;
            return lines;
        }

        public static int RandomInt(int min, int max)
        {
            return random.Next(min, max);
        }

        public static void EmptyDirectory(string path)
        {
            Trace.WriteLine("Trying to empty directory: " + path);

            var allFiles = new DirectoryInfo(path).GetFiles();
            foreach (var item in allFiles)
            {
                try
                {
                    item.Delete();
                }
                catch (Exception e)
                {

                    Trace.WriteLine("Error: " + e.Message);
                }
            }
            var allDirs = new DirectoryInfo(path).GetDirectories();
            foreach (var item in allDirs)
            {
                EmptyDirectory(item.FullName);
                try
                {
                    if (item.GetFiles().Count() > 0)
                    {
                        item.Delete();
                    }
                }
                catch (Exception e)
                {

                    Trace.WriteLine("Error: " + e.Message);
                }

            }


        }

        internal static byte[] RandomBytes(int byteSize)
        {
            byte[] bytes = new byte[byteSize];
            random.NextBytes(bytes);
            return bytes;
        }
    }
}
